﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using System.IO;
using Charities.ModelViews;

namespace Charities.Forms
{
    public partial class WorthiesNamesReport : Form
    {
        int userId;
       public bool is_loaded = false;
       ModelContainer ctx = new ModelContainer();
       Form fo = null;
        public WorthiesNamesReport(int uid, Form fm)
        {
            fo = fm;
            userId = uid;
            MdiParent = fm;
            InitializeComponent();
        }

        private void WorthiesNamesReport_Load(object sender, EventArgs e)
        {
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            is_loaded = true;
            
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            var allWorthies = ctx.Worthies.ToList();
            if (dtp_FromDate.Focused)
            {
                var fromWorthies = allWorthies.
                    Where(c => c.InsertDate.Value.Year >= dtp_FromDate.Value.Year
                    && c.InsertDate.Value.Month >= dtp_FromDate.Value.Month
                    && c.InsertDate.Value.Day >= dtp_FromDate.Value.Day).ToList();
                allWorthies = fromWorthies;
          
            }
            if (dtp_ToDate.Focused)
            {
                var toWorthies = allWorthies.
                   Where(c => c.InsertDate.Value.Year <= dtp_FromDate.Value.Year
                && c.InsertDate.Value.Month <= dtp_FromDate.Value.Month
                && c.InsertDate.Value.Day <= dtp_FromDate.Value.Day).ToList();
                allWorthies = toWorthies;
            }
            List<ModelViews.WorthiesView> wView = new List<ModelViews.WorthiesView>();
            foreach (var w in allWorthies)
            {
                Person p = ctx.People.First(c => c.Id == w.PersonId);
                ModelViews.WorthiesView wv = new ModelViews.WorthiesView();
                wv.Id = (int)p.WorthyId;
                wv.insertDate = p.InsertDate;
                wv.OreginalAddress = w.OriginalAddress;
                wv.RelativesCount = w.People.Count();
                wv.CurrentAddress = w.CurrentAddress;
                wv.DisplayName = p.FirstName + " " + p.FatherName + " " + p.LastName;
                if (dtp_FromDate.Focused)
                    wv.FromDate = dtp_FromDate.Value;
                else
                    wv.FromDate = (DateTime)allWorthies.First().InsertDate;
                if (dtp_ToDate.Focused)
                    wv.ToDate = dtp_ToDate.Value;
                else
                    wv.ToDate = (DateTime)allWorthies.Last().InsertDate;
                wv.WorthyTypeName = w.WorthyType.Name;
                wv.PhoneNumber = w.Phone;
                wView.Add(wv);
            }
            WorthiesViewBindingSource.DataSource = wView;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.reportViewer1.RefreshReport();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void WorthiesNamesReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void WorthiesNamesReport_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
